package com.example.test_8_video;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class VideoViewActivity extends AppCompatActivity implements View.OnClickListener {
    private VideoView video;
    ImageButton start,pause,return_1,stop;
    TextView currentTime1,totalTime,sound1,sound2;
    SeekBar seekBar_1,seekBar_2;
    AudioManager mAudioManager;
    //MyHandler myHandler;
    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
        public void run() {
            if (video.isPlaying()) {
                int current = video.getCurrentPosition();
                seekBar_1.setProgress(current);
                currentTime1.setText(time(video.getCurrentPosition()));
            }
            handler.postDelayed(runnable, 1000);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_view);
        Intent intent = getIntent();
        final String path = intent.getStringExtra("path");
        final File videoFile=new File(path);
        video = (VideoView)findViewById(R.id.video);
        requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.INTERNET},0x123);
        //四个按钮实例化
        video.setVideoPath(videoFile.getAbsolutePath());
        start =(ImageButton)findViewById(R.id.start);
        pause =(ImageButton) findViewById(R.id.pause);
        return_1 =(ImageButton) findViewById(R.id.reture_1);
        stop =(ImageButton) findViewById(R.id.stop);
        start.setOnClickListener(this);
        pause.setOnClickListener(this);
        return_1.setOnClickListener(this);
        stop.setOnClickListener(this);
        //myHandler = new MyHandler();
        //声音拖动条设置
        sound1 =(TextView)findViewById(R.id.sound2);
        sound2=(TextView)findViewById(R.id.sound3);
        mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
        seekBar_2 = (SeekBar)findViewById(R.id.process1);
        final int maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        sound2.setText(maxVolume+" ");
        seekBar_2.setMax(maxVolume);
        int currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        sound1.setText(currentVolume+"");
        seekBar_2.setProgress(currentVolume);//设置当前进度
        seekBar_2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
                int currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
                seekBar.setProgress(currentVolume);
                sound1.setText(currentVolume + "");//不能单独放currentVolume
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {}
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {}
        });
        //视频进度条的设置
        currentTime1 =(TextView)findViewById(R.id.textViewCurrent);//视频播放的当前时间
        totalTime = (TextView)findViewById(R.id.totalTime);//视屏的总时长
        seekBar_1 = (SeekBar)findViewById(R.id.process);
        seekBar_1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (video.isPlaying()) {
                    //移动当前播放的位置
                    video.seekTo(progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        if (requestCode==0x123&&grantResults[0]== PackageManager.PERMISSION_GRANTED)
        {
            Toast.makeText(this,"申请权限成功！",Toast.LENGTH_SHORT).show();
        }
    }
    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.start:
                play();
                totalTime.setText(time(video.getDuration())+" ");
                Toast.makeText(this,"开始播放视频",Toast.LENGTH_SHORT).show();
                break;
            case R.id.pause:
                video.pause();
                Toast.makeText(this,"暂停播放视频",Toast.LENGTH_SHORT).show();
                break;
            case R.id.reture_1:
                video.seekTo(0);
                video.start();
                break;
            case R.id.stop:
                video.stopPlayback();
                Toast.makeText(this,"停止播放视频",Toast.LENGTH_SHORT).show();
                break;
        }
    }
/*    //创建MyHandler类来配合进度条的更新
    class MyHandler extends Handler {
        public MyHandler(){}

        public MyHandler(Looper looper) {
            super(looper);
        }
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what==0x123) {
            }
        }
    }*/
//    //更新进度条的线程
//    class Thread_1 implements Runnable{
//        @Override
//        public void run() {
//            if (video.isPlaying())
//            {
//                int currentTime = video.getCurrentPosition();
//                seekBar_1.setProgress(currentTime);
//                currentTime1.setText(time(currentTime));
//                Message message = null;
//                Bundle bundle = new Bundle();
//                bundle.putInt("currentTime",currentTime);
//                message.setData(bundle);
//                message.what = 0x123;
//                VideoViewActivity.this.myHandler.sendMessage(message);
//                try {
//                    Thread.sleep(500);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            }
//            handler.postDelayed(T, 500);
//        }
//    }
    //视屏播放
    public void play()
    {
        if (!video.isPlaying())
        {
            video.start();
            handler.postDelayed(runnable,0);
            seekBar_1.setMax(video.getDuration());
        }
    }
    //将获取的视频位置转换为时间类
    protected String time(long millionSeconds) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss");
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(millionSeconds);
        return simpleDateFormat.format(c.getTime());
    }
}
